package com.szqhyxj.youxiangju.biz.merchantHome.shop.commoditeDetails;

import com.szqhyxj.youxiangju.biz.merchantHome.bean.CommoditeDetails;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.ShopModel;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

class CommoditeDetailsPresenter extends AbBasePresenter implements CommoditeDetailsContract.Presenter {

    private final CommoditeDetailsContract.View view;
    private CommoditeDetails data;

    public CommoditeDetailsPresenter(CommoditeDetailsContract.View view){
        this.view = view;
    }

    @Override
    public void init(String packageId){
        addDisposables(ShopModel.getPosPackage(packageId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
        .subscribe(new Consumer<BaseResult<CommoditeDetails>>(){
            @Override
            public void accept(BaseResult<CommoditeDetails> listBaseResult) throws Exception{
                view.dismissLoading();
                data = listBaseResult.getData();
                if(listBaseResult.businessLogicSuccess()){
                    view.showCommodite(data);
                }else {
                     view.showToast(listBaseResult.getMessage());
                }
            }
        }, new Consumer<Throwable>(){
            @Override
            public void accept(Throwable throwable) throws Exception{
                view.showToast(throwable.getMessage());
                view.dismissLoading();
            }
        }));
    }

    @Override
    public void save(){
        view.navPurchase(data);
    }
}